<?php
if (!defined('ABSPATH')) {
	define('ABSPATH', dirname(dirname(__FILE__)) . '/');
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';
require_once ABSPATH . DRINC . '/dr-themes.php';
require_once ABSPATH . DRINC . '/mygravatar.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

$tag_id      = isset($_GET['tid'])   ? intval($_GET['tid']) : 0;
$order       = isset($_GET['order']) ? dr_get('order') : "time";
$list        = isset($_GET['list'])  ? dr_get('list') : "allquestion";
$id          = isset($_GET['vid'])   ? intval($_GET['vid']) : 0;
$vote_type   = isset($_GET['type'])  ? $_GET['type'] : '';
$page        = isset($_GET['page'])  ? intval($_GET['page']) : 1;

// 获得登陆用户的ID
$user_id = dr_is_login_on();

// 问题的排序的考虑因素
$orderlist = array('time', 'answers', 'marks', 'votes');

if (!in_array($list, array('answered', 'unanswered', 'bested', 'unbested', 'allquestion'))) {
	$list = '';
}

/*****************获取问题列表*****************************/

//发送HTTP协议，获得所有问题的列表
$posts = array(
	'action'      => 'get_question_list',
	'tag_id'      => $tag_id,
	'order'       => $order,
	'list'        => $list,
	'login_id'    => $user_id,
	'page'        => $page
);

$result       = dr_server_request('dr-app-questions.php', $posts);
$result_array = json_decode($result, 1);

$pages            = $result_array[0];
$question_list    = $result_array[1];
$setting          = $result_array[2];
$top_question_num = $result_array[3];

//将数据转换成html格式后输出
dr_to_html($question_list);

foreach ($question_list as &$question) {
	$question['content'] = strip_tags($question['content']);
	$question['user_avatar'] = (string) new MyGravatar($question['user_email'], 32);
}


// 给页面变量赋值
$smarty->assign('page_title',    get_questions_page_title($list) . ' - ' . get_site_name());

$smarty->assign('list',          $list);
$smarty->assign('setting',       $setting);
$smarty->assign('pages',         $pages);
$smarty->assign('questions',     $question_list);
$smarty->assign('user_id',       $user_id);
$smarty->assign('topNum',        $top_question_num);
$smarty->assign('question_page', $page);

$content = array();
$content['top_navs'] = get_page_top_navigation();
$content['menus'] = get_page_menus('questions');
$content['tabs'] = get_page_tabs('questions-old', $list);
$content['filters'] = get_page_filters('questions-old', $list, $order);


if ($tag_id !== 0) {
	$tag_detail = dr_get_tag_detail($tag_id);

	$content['tabs']['title'] = "\"{$tag_detail['title']}\"标签的问题";
	foreach ($content['tabs']['tabs'] as &$tab) {
		$tab['link'] .= '&tid=' . $tag_detail['tag_id'];
	}
	foreach ($content['filters']['tabs'] as &$filter) {
		$filter['link'] .= '&tid=' . $tag_detail['tag_id'];
	}
}


// 显示页面
$smarty->assign('content', $content);
$smarty->display('questions.tpl');


/* ====Functions==== */

function dr_get_tag_detail($id) {
	$posts = array(
		'action'      => 'get_tag_detail',
		'id'      => $id,
	);

	$result       = dr_server_request('dr-app-tags.php', $posts);

	return json_decode($result, 1);
}